package com.sq.universe.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import  com.sq.universe.base.controller.BaseController;
import  com.sq.universe.base.entity.RespEntity;
import  com.sq.universe.system.entity.FileCategoryEntity;
import com.sq.universe.system.entity.MenuEntity;
import  com.sq.universe.system.service.FileCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@RestController
@RequestMapping("/fileCategory")
@CrossOrigin
public class FileCategoryController extends BaseController {

    @Autowired
    private FileCategoryService fileCategoryService;

    /**
    * 查询数据
    * @param fileCategoryEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("system:fileCategory:query")
    public RespEntity query(@RequestBody(required = false) FileCategoryEntity fileCategoryEntity){
        try {
            return new RespEntity("查询成功",fileCategoryService.query(fileCategoryEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param fileCategoryEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("system:fileCategory:queryByPage")
    public RespEntity queryByPage(@RequestBody(required = false) FileCategoryEntity fileCategoryEntity){
        try {
            PageHelper.startPage(fileCategoryEntity.getPageNo(),fileCategoryEntity.getPageSize());
            List<FileCategoryEntity> fileCategoryList = fileCategoryService.query(fileCategoryEntity);
            PageInfo<FileCategoryEntity> pageInfo = new PageInfo<FileCategoryEntity>(fileCategoryList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param fileCategoryEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("system:fileCategory:insert")
    public RespEntity insert(@RequestBody(required = false) FileCategoryEntity fileCategoryEntity){
        try {
            fileCategoryEntity.setFcCreateTime(new Date());
            fileCategoryEntity.setFcCreateUser(this.getCurrentUser().getUserId());
            fileCategoryService.insert(fileCategoryEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param fileCategoryEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("system:fileCategory:update")
    public RespEntity update(@RequestBody(required = false) FileCategoryEntity fileCategoryEntity){
        try {
            fileCategoryEntity.setFcUpdateTime(new Date());
            fileCategoryEntity.setFcUpdateUser(this.getCurrentUser().getUserId());
            fileCategoryService.update(fileCategoryEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param fileCategoryEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("system:fileCategory:delete")
    public RespEntity delete(@RequestBody(required = false) FileCategoryEntity fileCategoryEntity){
        try {
            if(fileCategoryEntity.getFcId() == null){
                return new RespEntity("删除失败");
            }
            fileCategoryEntity.setFcUpdateTime(new Date());
            fileCategoryEntity.setFcUpdateUser(this.getCurrentUser().getUserId());
            fileCategoryService.logicalDelete(fileCategoryEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("删除失败");
        }
    }

    /**
    * 根据ID查询
    * @param fileCategoryEntity
    * @return
    */
    @PostMapping("/getById")
    @RequiresPermissions("system:fileCategory:getById")
    public RespEntity getById(@RequestBody(required = false) FileCategoryEntity fileCategoryEntity){
        try {
            if(fileCategoryEntity.getFcId() == null){
                return new RespEntity("请输入id查询");
            }
            return new RespEntity("查询成功",fileCategoryService.getById(fileCategoryEntity.getFcId()));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 根据实体查询
    * @param fileCategoryEntity
    * @return
    */
    @PostMapping("/get")
    @RequiresPermissions("system:fileCategory:get")
    public RespEntity get(@RequestBody(required = false) FileCategoryEntity fileCategoryEntity){
        try {
            return new RespEntity("查询成功",fileCategoryService.get(fileCategoryEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
     * 查询树
     * @param fileCategoryEntity
     * @return
     */
    @PostMapping("/queryTree")
    @RequiresPermissions("system:fileCategory:queryTree")
    public RespEntity queryTree(@RequestBody(required = false) FileCategoryEntity fileCategoryEntity){
        try {
            return new RespEntity("查询成功",fileCategoryService.queryTree(fileCategoryEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

}